package com.yanqu.road.logic.service.impl;

import com.yanqu.road.entity.logAnalysis.RoleShopBuy;
import com.yanqu.road.logic.service.RoleShopBuyService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author duli
 * @version 1.0
 * @date 2020/9/17 17:30
 * @Description 商城购买
 */
@Service
public class RoleShopBuyServiceImpl implements RoleShopBuyService {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void batchSave(List<RoleShopBuy> roleShopBuyList, String serviceId) {
        if (CollectionUtils.isNotEmpty(roleShopBuyList)) {
            jdbcTemplate.batchUpdate("INSERT INTO role_shop_buy (app_id,device_id,source_id,source_name,user_id,user_name,server_id,server_name," +
                            "role_id,role_name,type_item_id,type_item_name,item_id,item_name,item_num,consume_num,created_time) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    roleShopBuyList, roleShopBuyList.size(), (ps, d) -> {
                        ps.setInt(1, d.getAppId());
                        ps.setString(2, d.getDeviceId());
                        ps.setString(3, d.getSourceId());
                        ps.setString(4, d.getSourceName());
                        ps.setString(5, d.getUserId());
                        ps.setString(6, d.getUserName());
                        ps.setString(7, d.getServerId());
                        ps.setString(8, d.getServerName());
                        ps.setString(9, d.getRoleId());
                        ps.setString(10, d.getRoleName());
                        ps.setString(11, d.getTypeItemId());
                        ps.setString(12, d.getTypeItemName());
                        ps.setString(13, d.getItemId());
                        ps.setString(14, d.getItemName());
                        ps.setInt(15, d.getItemNum());
                        ps.setFloat(16, d.getConsumeNum());
                        ps.setInt(17, d.getCreatedTime());
                    });
        }
    }

    @Override
    public void batchBeforeSave(List<RoleShopBuy> roleShopBuyList) {
        Map<String, List<RoleShopBuy>> dataMap = roleShopBuyList.stream().collect(Collectors.groupingBy(e -> e.getServerId()));
        for (String serverId : dataMap.keySet()) {
            batchSave(dataMap.get(serverId), serverId);
        }
    }
}
